package com.caits.lbs.framework.services.mail;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Properties;

import javax.mail.Message;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

import org.apache.logging.log4j.Logger;

import com.caits.lbs.framework.log.CommonLogFactory;

/**
 * <p>
 * JavaMailSendImpl
 * </p>
 * <p>
 * 用途：这只是一个测试类，调用原生的javax.mail包
 * 有些bug，内容为base64，标题不出来，推荐使用MailSenderImpl
 * 该包容易与geronimo-javamail_1.4_spec-*.jar冲突，切忌!
 * </p>
 * 
 * @author 周华彬(zhouhuabin@ctfo.com, zhou_hua_bin@163.com)
 * @version 0.0.1 2011-8-16
 *          <table style="border:1px solid gray;">
 *          <tr>
 *          <th width="100px">版本号</th>
 *          <th width="100px">动作</th>
 *          <th width="100px">修改人</th>
 *          <th width="100px">修改时间</th>
 *          </tr>
 *          <!-- 以 Table 方式书写修改历史 -->
 *          <tr>
 *          <td>0.0.0</td>
 *          <td>创建类</td>
 *          <td>zhb</td>
 *          <td>2011-8-16 下午6:51:49</td>
 *          </tr>
 *          <tr>
 *          <td>0.0.1</td>
 *          <td>创建类</td>
 *          <td>zhb</td>
 *          <td>2011-8-16 下午6:51:49</td>
 *          <td>0.0.2</td>
 *          <td>修改类</td>
 *          <td>xxx</td>
 *          <td>x年x月x日</td>
 *          </tr>
 *          </table>
 */
public class JavaMailSendImpl implements IMailService {

	public static Logger log = CommonLogFactory.getLog();

	private final static String smtphost = "mail.ctfo.com";// 邮局服务器地址

	private final static String smtpUserName = "zhouhuabin";// 邮件服务器登录用户名

	private final static String smtpUserPass = "790925";// 邮件服务器登录密码

	private final static String from = "zhouhuabin@ctfo.com";// 发邮件的邮箱

	@Override
	public void sendTextMail(String toAddress, String subject, String text) {
		// TODO Auto-generated method stub

	}

	@Override
	public void sendHTMLMail(String toAddress, String subject, String text) {
		// TODO Auto-generated method stub

	}

	@Override
	public void sendAttachmentMail(String toAddress, String subject,
			String text, List<String> files) {
		// TODO Auto-generated method stub

	}

	public boolean send(Mail mail) throws Exception {
		try {
			Transport transport = null;
			Properties props = new Properties();
			props.put("mail.transport.protocol", "smtp");
			props.put("mail.smtp.host", smtphost);

			props.put("mail.smtp.connectiontimeout", "10000"); //
			props.put("mail.smtp.timeout", "10000");
			props.put("mail.smtp.auth", "true");
			Session ssn = Session.getInstance(props, null);
			MimeMessage message = new MimeMessage(ssn);
			InternetAddress fromAddress = new InternetAddress(mail.getFrom());
			message.setFrom(fromAddress);
			InternetAddress toAddress = new InternetAddress(mail.getTo());
			message.addRecipient(Message.RecipientType.TO, toAddress);
			message.setSubject(mail.getSubject());
			message.setContent(mail.getContent(), "text/html;charset=gbk");
			try {
				transport = ssn.getTransport("smtp");
				transport.connect(smtphost, smtpUserName, smtpUserPass);
			} catch (RuntimeException e) {
				e.printStackTrace();
				return false;
			}
			if (!transport.isConnected()) {
				log.error("连接超时" + smtphost);
				return false;
			}
			transport.sendMessage(message,
					message.getRecipients(Message.RecipientType.TO));
			transport.close();
			log.info("邮件发送成功！");
		} catch (Exception e) {
			log.error("邮件发送错误",e);
		}
		return true;
	}

	/**
	 * FIXME
	 * 
	 * @param args
	 */
	public static void main(String[] args) {
		String from = "zhb@ctfo.com";
		String to = "zhouhuabin@ctfo.com";
		String subject = "测试,原生javamail发送";
		String content = "中文试试";
		String ccusers = null;
		ArrayList attachedFileList = null;
		Date date = new Date();
		Mail mail = new Mail();
		mail.setFrom(from);
		mail.setTo(to);
		mail.setSubject(subject);
		mail.setContent(content);
		mail.setDate(date);
		mail.setAttachedFileList(attachedFileList);
		JavaMailSendImpl sender = new JavaMailSendImpl();
		try {
			boolean ret = sender.send(mail);
			if (ret)
				System.out.println(String.format("发送邮件给%s成功", to));
			else
				System.out.println(String.format("发送邮件给%s失败", to));
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

}

class Mail {

	private String from = null;
	private String to = null;
	private String subject = null;
	private String content = null;
	private String ccusers = null;
	private ArrayList attachedFileList = null;
	private Date date = null;

	public Mail() {
		// TODO Auto-generated constructor stub
	}

	public ArrayList getAttachedFileList() {
		return attachedFileList;
	}

	public void setAttachedFileList(ArrayList attachedFileList) {
		this.attachedFileList = attachedFileList;
	}

	public String getContent() {
		return content;
	}

	public void setContent(String content) {
		this.content = content;
	}

	public String getFrom() {
		return from;
	}

	public void setFrom(String from) {
		this.from = from;
	}

	public String getSubject() {
		return subject;
	}

	public void setSubject(String subject) {
		this.subject = subject;
	}

	public String getTo() {
		return to;
	}

	public void setTo(String to) {
		this.to = to;
	}

	public Date getDate() {
		return date;
	}

	public void setDate(Date date) {
		this.date = date;
	}

	public String getCcusers() {
		return ccusers;
	}

	public void setCcusers(String ccusers) {
		this.ccusers = ccusers;
	}

}
